package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.domain.Setmeal;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {


    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //设置用户id，指定当前是哪个用户的购物车数据
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,id);
        //根据口味

        lqw.eq(shoppingCart.getDishFlavor()!=null, ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());

        //添加到购物车的是菜品
        if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }
        //添加到购物车的是套餐
        if(setmealId!=null){
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = getOne(lqw);
        if(one!=null){
            one.setNumber(one.getNumber()+1);

            updateById(one);
            return one;
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        save(shoppingCart);
        return shoppingCart;
    }

    @Override
    public List<ShoppingCart> listAll() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = list(lqw);

        return list;
    }

    @Override
    public void deleteAll() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        remove(lqw);
    }

    @Override
    public ShoppingCart updateGou(ShoppingCart shoppingCart) {
        //如果是菜品
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(null != shoppingCart.getDishId(),ShoppingCart::getDishId,shoppingCart.getDishId());
            wrapper.eq(null != shoppingCart.getSetmealId(),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            wrapper.eq(null!=shoppingCart.getDishFlavor(),ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            wrapper.eq( ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart one = getOne(wrapper);
            Integer number = one.getNumber();
                //如果菜品数量为1，就删除此记录
                if(number==1){
                    removeById(one.getId());
                    one =shoppingCart;
                    return one;
            }
            //修改数量
            one.setNumber(one.getNumber()-1);
            updateById(one);
            return one;
        }
    }
